@tool
class_name ContextBehavior_AvoidCollision
extends BaseContextBehavior

#region 属性
@export var force:float
@export var avoid_radius:float = 30
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init():
	super()
func calc_steering(behaviors_owner:Node,agent:SteeringBehaviorAgent,delta:float):
	super(behaviors_owner,agent,delta)
	var detect_component:DetectComponent = agent.entity.get_component(DetectComponent)
	if not detect_component:
		return
	var detector_obstacle:Detector_Obstacle = detect_component.get_detector(GameEnum.DetectorType.Obstacle)
	if not detector_obstacle:
		return
	var obstacle_closest_point:Array = detector_obstacle.get_points(agent.get_pos(),avoid_radius,Callable(),-1)
	for point in obstacle_closest_point:
		var dir = (point - agent.get_pos()).normalized()
		var distance = (point - agent.get_pos()).length()
		agent.add_linear_force_interest(-dir * 1.0/distance * sensitive * force)
		#if entity.test_move(entity.global_transform,entity.entity_data.get_scale_move_speed() * directions[i] * steering_behavior_manager.update_dalay):
			#danger[i] += 1/(entity.entity_data.get_scale_move_speed() * steering_behavior_manager.update_dalay) * sensitive
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _init() -> void:
	if Engine.is_editor_hint():
		resource_local_to_scene = true
	behavior_type = GameEnum.ContextBehaviorType.AvoidCollision
func draw() -> void:
	super.draw()
	GameSteeringBehaviorDebug.draw_circle(agent.global_position,avoid_radius,Color.RED,false)
#endregion
